<template>
  <div>
    <Goback></Goback>
    <h1>购物车</h1>
    <div class="container">
        <div class="items-msg">
          <input type="checkbox" class="check-box" v-model="allcheck" @click="checkall()">
          <p>商品信息</p>
          <p>操作</p>
        </div>
        <!-- <div class="items-msg" v-for="(item,i) of items" :key="i">
          <button class="btn"></button>
          <img :src="item.imgurl" alt="" class="item-img">
          <p>￥{{item.price}}</p>
          <p class="item-desc">{{item.shopdesc}}</p>
          <p><button>-</button><span>{{item.count}}</span><button>+</button></p>
        </div> -->
              <div class="items-msg" v-for="(item,i) of items" :key="i">
          <input type="checkbox" class="check-box" :value="i" v-model="check" @change="additem(i)" />
          <div class="middle-msg">
             <img :src="item.imgurl" alt="" class="item-img">
          <p>￥{{item.price}}</p>
          <p class="item-desc">{{item.shopdesc}}</p>
          </div>
         
          <p class="item-right"><button @click="reduce(i,item.count),item.count--" >-</button><span>{{item.count}}</span><button @click="item.count++">+</button></p>
        </div>
        <!-- <div class="allprice">
          <span>合计:￥{{allprice}}</span>
        </div> -->
    </div>
    <footer class="footbar">
      <div class="footbar-left">
              <h3>￥{{allprice}}</h3>
      <p>商品:<span>{{allcount}}</span></p>
      </div>
    <div class="footbar-right" @click="additem">提交订单</div>
    </footer>
  </div>

</template>

<script>
export default {
  mounted(){
    this.getcart();
  },
  data(){
    return{
      items:[],//包括没选中的
      check:[],
      allcheck:false,
      alllist:[]
    }
  },
  computed:{
    allprice(){
      let allprice=0
      this.check.forEach((ele)=>{
      allprice+=this.items[ele].price
      })
      return allprice
    },
    allcount(){
          let allcount=0
      this.check.forEach((ele)=>{
      allcount+=this.items[ele].count
      })
      return allcount
    }
  },
  methods:{
    additem(){
      // console.log(this.allcheck)
      if(this.check.length==this.items.length){
        this.allcheck=true
      }else{
        this.allcheck=false
      }
      },
    checkall(){
     let checkalllist=[];
      if(!this.allcheck){
        for(var key in this.items){
       checkalllist.push(key);
       this.check=checkalllist
     }
      }
      else{
        this.check=[];
      }
     
    },
    reduce(i,count){
      count--;
      if(count==0){
        this.items.splice(i,1)
      }
    },
    getcart(){
      this.items=this.$store.state.shopcart
    }
  }
}
</script>

<style lang="scss" scoped>
.footbar{
  position:fixed;
  width:100%;
  left:0;
  bottom: 0;
  height:3rem;
  display: flex;
  .footbar-left{
    height: 100%;
    width:100%;
    display: flex;
    background: #fff;
    flex-direction: column;
    border: 1px solid #cebcbc;
    // align-items: center;
    h3{
      color:red;
      margin:0;
    }
    p{
      margin:0;
    }
    span{
      color:red
    }
  }
  .footbar-right{
    height:100%;
    width:40%;
    line-height: 3rem;
    text-align: center;
    background-color: red;
    color:#fff
  }
}
.check-box{
  // border-radius: 10px;
  height:1.2rem;
  width: 1.2rem;
  outline: none;
}
.item-right{
  display: contents;
}
.item-img{
  width: 40px;
  padding:10px
}
.item-desc{
  box-sizing: border-box;
  padding: 5px;
  font-size: .7rem;
}
.middle-msg{
  display: contents;
  width: 70%;
  justify-content: space-around;
}
.container{
  display: flex;
  flex-direction: column;
  align-items: center;
  .items-msg{
    .btn{
       width: 5px;
      height:1rem;
      text-align: center;
    }
    width:100%;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
}
</style>